package com.ruoyi.web.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.util.StringUtil;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.ProjectTaskStatus;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.BProjectTask;
import com.ruoyi.system.domain.request.ProjectRequest;
import com.ruoyi.system.domain.request.ProjectTaskRequest;
import com.ruoyi.system.domain.vo.*;
import com.ruoyi.system.service.BProjectService;
import com.ruoyi.system.service.BProjectTaskService;
import com.ruoyi.system.service.BTaskScoreService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.Date;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/project")
public class ProjectController extends BaseController {

    @Autowired
    BProjectService projectService;

    @Autowired
    BProjectTaskService projectTaskService;

    @Autowired
    BTaskScoreService taskScoreService;

    /**
     * 统计任务数
     */
    @GetMapping(value = "/task/getData")
    public AjaxResult getData() {
        TaskDataVo taskDataVo = projectService.getData(SecurityUtils.getUserId());
        return success(taskDataVo);
    }

    /**
     * 分页获取所属任务的项目列表
     */
    @GetMapping("/page")
    public TableDataInfo queryPage(ProjectRequest request){
        request.setUserId(SecurityUtils.getUserId());
        startPage();
        List<BProjectVo> list = projectService.queryPage(request);
        return getDataTable(list);
    }

    /**
     * 获取我的任务列表
     */
    @GetMapping("/task/list")
    public TableDataInfo getTaskList(ProjectTaskRequest request){
        request.setUserId(SecurityUtils.getUserId());
        List<BProjectTaskVo> list = projectTaskService.getTaskList(request);
        return getDataTable(list);
    }


    /**
     * 根据任务Id获取详细信息
     */
    @GetMapping(value = "/task/{taskId}")
    public AjaxResult getInfo(@PathVariable Long taskId) {
        BProjectTaskVo taskVo = projectTaskService.getInfo(taskId);
        return success(taskVo);
    }

    /**
     * 提交任务训练
     * @param taskVo
     * @return
     */
    @PostMapping("/task/train")
    public AjaxResult train(@Validated @RequestBody BProjectTaskVo taskVo){
        BProjectTask task = projectTaskService.getOne(new QueryWrapper<BProjectTask>().lambda()
                .eq(BProjectTask::getId,taskVo.getId())
                .eq(BProjectTask::getTrainDoctorId,SecurityUtils.getUserId()));
        if(ObjectUtil.isEmpty(task)){
            throw new ServiceException("您没有权限操作此任务");
        }
        String status = task.getStatus();
        if(status.equals(ProjectTaskStatus.COMPLETE.getValue())){//已完成训练
            throw new ServiceException("您已完成训练");
        }
        if(status.equals(ProjectTaskStatus.AUDIT_PASS.getValue())){//已通过审核
            throw new ServiceException("您已完成训练，并已通过审核");
        }
        //保存评分信息
        boolean result = taskScoreService.saveScore(taskVo);
        if(result){//保存评分信息成功，则更新任务状态
            task.setStatus(ProjectTaskStatus.COMPLETE.getValue());//设置已完成训练
            task.setContent(taskVo.getContent());
            task.setUpdateTime(new Date());
            projectTaskService.updateById(task);
        }
        return success();
    }

    /**
     * 提交任务审核
     * @param taskVo
     * @return
     */
    @PostMapping("/task/review")
    public AjaxResult review(@Validated @RequestBody BProjectTaskVo taskVo){
        BProjectTask task = projectTaskService.getOne(new QueryWrapper<BProjectTask>().lambda()
                .eq(BProjectTask::getId,taskVo.getId())
                .eq(BProjectTask::getReviewDoctorId,SecurityUtils.getUserId()));
        if(ObjectUtil.isEmpty(task)){
            throw new ServiceException("您没有权限操作此任务");
        }
        String status = task.getStatus();
        if(!status.equals(ProjectTaskStatus.COMPLETE.getValue())){//只能审核已完成的任务
            throw new ServiceException("只能审核已完成训练的任务");
        }
        task.setStatus(taskVo.getStatus());
        task.setUpdateTime(new Date());
        task.setReviewTime(new Date());
        if(StringUtil.isNotEmpty(taskVo.getReviewRemark())){
            task.setReviewRemark(taskVo.getReviewRemark());
        }
        projectTaskService.updateById(task);
        return success();
    }
}
